<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyWord" placeholder="店铺名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getShops">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--店铺信息显示列表-->
		<el-table :data="shops" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="logo" label="店铺logo" width="120" sortable>
				<!--<img :src="" width="200"/>-->
			</el-table-column>
			<el-table-column prop="name" label="店铺名字" width="120" sortable>
			</el-table-column>
			<el-table-column prop="tel" label="联系方式" width="120" sortable>
			</el-table-column>
			<el-table-column prop="registerTime" label="注册时间" width="120" sortable>
			</el-table-column>
			<el-table-column prop="address" label="地址" width="120" sortable>
			</el-table-column>
			<el-table-column prop="admin.username" label="店主" width="120" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100" sortable>
				<template scope="scope"> <!--显示状态信息-->
					<span style="color: green" v-if="scope.row.state == 0">待审核</span>
					<span style="color: greenyellow" v-else-if="scope.row.state == 1">驳回</span>
					<span style="color: gray" v-else-if="scope.row.state == 2">正常</span>
					<span style="color: red" v-else-if="scope.row.state == -1">禁用</span>
					<span v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
					<el-button size="small" @click="audit(scope.$index, scope.row)">审核</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--审核状态框-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="状态">
					<el-radio-group v-model="editForm.state">
						<el-radio class="radio" :label="0">待审核</el-radio>
						<el-radio class="radio" :label="1">驳回</el-radio>
						<el-radio class="radio" :label="2">正常</el-radio>
						<el-radio class="radio" :label="-1">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getShopListPage, removeShop, batchRemoveShop, editShop, addShop } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
                    keyWord: ''
				},
				shops: [],
				total: 0,
                currentPage: 1,
				pageSize: 5,
				listLoading: false,
				sels: [],//列表选中列
                title: "",
				editFormVisible: false,// 审核状态是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},

                // 审核状态数据
                editForm: {
                    id: null,
                    name: '',
                    tel: '',
                    registerTime: null,
                    state: '',
                    address: '',
                    logo: '',
                    admin_id: '',
                },

				editFormVisible: false,//新增界面是否显示
				addLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
			}
		},
		methods: {
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getShops();
			},
			//获取店铺列表
			getShops() {
				let para = { // 分页+高级查询参数
					currentPage: this.currentPage,
					total: this.total,
					pageSize: this.pageSize,
                    keyWord: this.filters.keyWord // 高级查询参数
				};
				this.listLoading = true; // 忙等框开启
				/*发生post请求，分页请求，会被后端分页接口接收，并响应数据到result*/
				this.$http.post("/shop", para)
					.then(result=>{
					    this.shops = result.data.rows; // 填入分页数据
					    this.total = result.data.totals; // 填入数据总条数
                        this.listLoading = false; // 忙等框关闭
					}).catch(result=>{
					    console.debug(result)
						alert("系统繁忙")
				})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => { // 发送请求成功时执行
					this.listLoading = true; // 忙等框
					// 携带参数发送删除请求，
					this.$http.delete("/shop/" + row.id)
						.then(result=>{ // 请求发送成功时执行
                            this.listLoading = false; // 关闭忙等框
                            // 根据后端响应的对象进行判断
						    if (result.data.success) { // 删除成功时执行
                                this.$message({ // elementui中消息框
                                    message: result.data.msg, // 获取后端响应的删除成功信息
                                    type: 'success'
                                });
							} else {
                                this.$message({ // elementui中消息框
                                    message: result.data.msg, // 获取后端响应的删除成功信息
                                    type: 'error'
                                });
							}
                            this.currentPage = 1; // 给当前页赋值
						    this.getShops(); // 加载数据，回到第一页
						}).catch(result=>{
                        this.$message({
                            message: "系统繁忙。。。。",
                            type: 'error'
                        });
					})
				}).catch(() => {

				});
			},

			// 分页页面变化时
			selsChange: function (sels) {
				this.sels = sels;
			},

			//批量删除
			batchRemove: function () {
                var ids = this.sels.map(item => item.id) // sels:数组，将选择的数据的id值取出封装为一个数组
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true; // 开启忙等框
					this.$http.patch("/shop", ids)
						.then(result=>{
                            this.listLoading = false; // 关闭忙等框
							if (result.data.success) {
                                this.$message({
                                    message: result.data.msg,
                                    type: 'success'
                                });
							} else {
                                this.$message({
                                    message: result.data.msg,
                                    type: 'error'
                                });
							}
						}).catch(result=>{
                        this.$message({
                            message: "系统繁忙。。。",
                            type: 'error'
                        });
					})
                    this.getShops(); // 重新查询数据
				}).catch(() => {

				});
			},

            // 审核
            audit: function (index, row) {
                this.editFormVisible = true; // 显示模态框
                this.editForm = Object.assign({}, row); // 编辑回显数据
            },

			// 确认审核
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true; // 按钮忙等框
                            let para = Object.assign({}, this.editForm); // 克隆数据，将表单中的数据整合为一个对象
							if (para) {
							    para.admin_id = para.admin.id
							}
							console.debug(para)

                            this.$http.put("/shop", para)
                                .then(result => {
                                    this.editFormVisible = false; // 成功与否都关闭模态框
                                    this.editLoading = false; // 成功与否都关闭按钮模态框
                                    if (result.data.success) {
                                        this.$message({
                                            message: '提交成功',
                                            type: 'success'
                                        });
                                    } else {
                                        this.$message({
                                            message: result.data.msg,
                                            type: 'error'
                                        });
                                    }
                                    this.getShops(); // 重新查询数据
                                }).catch(result => {
                                this.$message({
                                    message: "系统繁忙。、。。。",
                                    type: 'error'
                                });
                            })
                        });
                    }
                });
            },

			// 获取图片
			getImgs(){
			},
		},
		mounted() {
			this.getShops();
			// this.getImgs()
		}
	}

</script>

<style scoped>

</style>